package com.sgb.member.view_model;

import android.util.Log;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.google.gson.Gson;
import com.sgb.member.MemberConfig;
import com.sgb.member.entity.ApplyCompanyBean;
import com.sgb.member.entity.CompanyDeptBean;
import com.sgb.member.entity.PersonApplyAndInviteBean;
import com.sgb.member.entity.PersonMemberBean;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;

public class PersonApplyJoinModel2 extends ViewModel {

    public MutableLiveData<String> msgLiveData = new MutableLiveData<>();
    public MutableLiveData<Boolean> isLoadLiveData = new MutableLiveData<>();
    public MutableLiveData<Boolean> showEmptyViewLiviData = new MutableLiveData<>();
    public MutableLiveData<Boolean> memberJoinSuccessLiveData = new MutableLiveData<>();
    public MutableLiveData<PersonMemberBean> memberProfileLiveData = new MutableLiveData<>();
    public MutableLiveData<List<CompanyDeptBean>> companyDeptBeanLiveData = new MutableLiveData<>();
    public MutableLiveData<List<CommonPopEntity>> levelLiveData = new MutableLiveData<>();


    //企业加盟个人  有审核
    public void memberJoinHasShenHe(PersonApplyAndInviteBean entity) {
       // Log.i("xxx","===="+new Gson().toJson(entity));
        isLoadLiveData.postValue(true);
        MemberConfig.getInstance().getApi().memberJoinApprove(entity).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                isLoadLiveData.postValue(false);
                if (data.getCode().equals("200")) {
                    memberJoinSuccessLiveData.postValue(true);
                } else {
                    msgLiveData.postValue(data.getMsg());
                }
            }
        });
    }



    //获取个人信息
    public void getPersonProfile(String no) {
        isLoadLiveData.postValue(true);
        MemberConfig.getInstance().getApi().getDetailsByUserNo(no).enqueue(new ICallBack<BaseEntity<PersonMemberBean>>() {
            @Override
            public void onSuccess(BaseEntity<PersonMemberBean> data) {
                isLoadLiveData.postValue(false);
                if(data != null &&  data.getData() != null){
                    memberProfileLiveData.postValue(data.getData());

                } else {
                    showEmptyViewLiviData.postValue(true);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<PersonMemberBean>> call, Throwable t) {
                super.onFailure(call, t);
                showEmptyViewLiviData.postValue(true);
            }
        });
    }

    // 审批【申请】部门列表
    public void getDeptList(String id) {
        MemberConfig.getInstance().getApi().getDeptList(id).enqueue(new ICallBack<BaseEntity<List<CompanyDeptBean>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CompanyDeptBean>> data) {
                List<CompanyDeptBean> tempList = data.getData();
                if (tempList != null && tempList.size() > 0) {
                    List<CompanyDeptBean> mPop3 = new ArrayList<>();
                    mPop3.addAll(tempList);
                    companyDeptBeanLiveData.postValue(mPop3);
                }
            }
        });
    }

    // 审批【申请】企业列表
    public void getMyCompanyList() {
        String companyNo = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, "");
        MemberConfig.getInstance().getApi().getMyCompanyList().enqueue(new ICallBack<BaseEntity<List<ApplyCompanyBean>>>() {
            @Override
            public void onSuccess(BaseEntity<List<ApplyCompanyBean>> data) {
                if (data != null && data.getData() != null) {
                    for (int i = 0; i < data.getData().size(); i++) {
                        ApplyCompanyBean bean = data.getData().get(i);
                        if (companyNo.equals(bean.getComNo())) {
                            getDeptList(bean.getId());
                            return;
                        }
                    }
                }

            }
        });
    }


    // 会员等级
    public  void getMemberLevelList() {
        MemberConfig.getInstance().getApi().getMemberLevelList().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                List<CommonPopEntity> tempList = data.getData();
                if (tempList != null && tempList.size() > 0) {
                    if (tempList.get(0).getTypeName().equals("全部")) {
                        tempList.remove(0);
                    }
                    List<CommonPopEntity>  mPop4 = new ArrayList<>();
                    mPop4.addAll(tempList);
                    levelLiveData.postValue(mPop4);
                }
            }
        });
    }

}
